<template>
  <el-dialog
    :title="!dataForm.rentId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户手机号" prop="houUserPhone">
      <el-input v-model="dataForm.houUserPhone" placeholder="用户手机号"></el-input>
    </el-form-item>
    <el-form-item label="房屋标题" prop="houseTitle">
      <el-input v-model="dataForm.houseTitle" placeholder="房屋标题"></el-input>
    </el-form-item>
    <el-form-item label="房屋价格" prop="housePrice">
      <el-input v-model="dataForm.housePrice" placeholder="房屋价格"></el-input>
    </el-form-item>
    <el-form-item label="租赁方式" prop="leaseType">
      <el-input v-model="dataForm.leaseType" placeholder="租赁方式"></el-input>
    </el-form-item>
    <el-form-item label="房屋布局" prop="houseLayout">
      <el-input v-model="dataForm.houseLayout" placeholder="房屋布局"></el-input>
    </el-form-item>
    <el-form-item label="房屋朝向" prop="houseOrientation">
      <el-input v-model="dataForm.houseOrientation" placeholder="房屋朝向"></el-input>
    </el-form-item>
    <el-form-item label="小区id" prop="estateId">
      <el-input v-model="dataForm.estateId" placeholder="小区id"></el-input>
    </el-form-item>
    <el-form-item label="默认图片" prop="houseImg">
      <el-input v-model="dataForm.houseImg" placeholder="默认图片"></el-input>
    </el-form-item>
    <el-form-item label="房屋面积" prop="houseSize">
      <el-input v-model="dataForm.houseSize" placeholder="房屋面积"></el-input>
    </el-form-item>
    <el-form-item label="楼栋号" prop="floorNum">
      <el-input v-model="dataForm.floorNum" placeholder="楼栋号"></el-input>
    </el-form-item>
    <el-form-item label="单元号" prop="unitNum">
      <el-input v-model="dataForm.unitNum" placeholder="单元号"></el-input>
    </el-form-item>
    <el-form-item label="门牌号" prop="houseNum">
      <el-input v-model="dataForm.houseNum" placeholder="门牌号"></el-input>
    </el-form-item>
    <el-form-item label="租期" prop="leaseTrem">
      <el-input v-model="dataForm.leaseTrem" placeholder="租期"></el-input>
    </el-form-item>
    <el-form-item label="房源类型" prop="houseType">
      <el-input v-model="dataForm.houseType" placeholder="房源类型"></el-input>
    </el-form-item>
    <el-form-item label="房屋状态" prop="houseStatus">
      <el-input v-model="dataForm.houseStatus" placeholder="房屋状态"></el-input>
    </el-form-item>
    <el-form-item label="出租人" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="出租人"></el-input>
    </el-form-item>
    <el-form-item label="出租人手机" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder="出租人手机"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="维护时间" prop="maintenanceTime">
      <el-input v-model="dataForm.maintenanceTime" placeholder="维护时间"></el-input>
    </el-form-item>
    <el-form-item label="配套设施" prop="houseFacility">
      <el-input v-model="dataForm.houseFacility" placeholder="配套设施"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          rentId: 0,
          houUserPhone: '',
          houseTitle: '',
          housePrice: '',
          leaseType: '',
          houseLayout: '',
          houseOrientation: '',
          estateId: '',
          houseImg: '',
          houseSize: '',
          floorNum: '',
          unitNum: '',
          houseNum: '',
          leaseTrem: '',
          houseType: '',
          houseStatus: '',
          userName: '',
          userPhone: '',
          createTime: '',
          maintenanceTime: '',
          houseFacility: ''
        },
        dataRule: {
          houUserPhone: [
            { required: true, message: '用户手机号不能为空', trigger: 'blur' }
          ],
          houseTitle: [
            { required: true, message: '房屋标题不能为空', trigger: 'blur' }
          ],
          housePrice: [
            { required: true, message: '房屋价格不能为空', trigger: 'blur' }
          ],
          leaseType: [
            { required: true, message: '租赁方式不能为空', trigger: 'blur' }
          ],
          houseLayout: [
            { required: true, message: '房屋布局不能为空', trigger: 'blur' }
          ],
          houseOrientation: [
            { required: true, message: '房屋朝向不能为空', trigger: 'blur' }
          ],
          estateId: [
            { required: true, message: '小区id不能为空', trigger: 'blur' }
          ],
          houseImg: [
            { required: true, message: '默认图片不能为空', trigger: 'blur' }
          ],
          houseSize: [
            { required: true, message: '房屋面积不能为空', trigger: 'blur' }
          ],
          floorNum: [
            { required: true, message: '楼栋号不能为空', trigger: 'blur' }
          ],
          unitNum: [
            { required: true, message: '单元号不能为空', trigger: 'blur' }
          ],
          houseNum: [
            { required: true, message: '门牌号不能为空', trigger: 'blur' }
          ],
          leaseTrem: [
            { required: true, message: '租期不能为空', trigger: 'blur' }
          ],
          houseType: [
            { required: true, message: '房源类型不能为空', trigger: 'blur' }
          ],
          houseStatus: [
            { required: true, message: '房屋状态不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '出租人不能为空', trigger: 'blur' }
          ],
          userPhone: [
            { required: true, message: '出租人手机不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          maintenanceTime: [
            { required: true, message: '维护时间不能为空', trigger: 'blur' }
          ],
          houseFacility: [
            { required: true, message: '配套设施不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.rentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rentId) {
            this.$http({
              url: this.$http.adornUrl(`/user/houserent/info/${this.dataForm.rentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.houUserPhone = data.houseRent.houUserPhone
                this.dataForm.houseTitle = data.houseRent.houseTitle
                this.dataForm.housePrice = data.houseRent.housePrice
                this.dataForm.leaseType = data.houseRent.leaseType
                this.dataForm.houseLayout = data.houseRent.houseLayout
                this.dataForm.houseOrientation = data.houseRent.houseOrientation
                this.dataForm.estateId = data.houseRent.estateId
                this.dataForm.houseImg = data.houseRent.houseImg
                this.dataForm.houseSize = data.houseRent.houseSize
                this.dataForm.floorNum = data.houseRent.floorNum
                this.dataForm.unitNum = data.houseRent.unitNum
                this.dataForm.houseNum = data.houseRent.houseNum
                this.dataForm.leaseTrem = data.houseRent.leaseTrem
                this.dataForm.houseType = data.houseRent.houseType
                this.dataForm.houseStatus = data.houseRent.houseStatus
                this.dataForm.userName = data.houseRent.userName
                this.dataForm.userPhone = data.houseRent.userPhone
                this.dataForm.createTime = data.houseRent.createTime
                this.dataForm.maintenanceTime = data.houseRent.maintenanceTime
                this.dataForm.houseFacility = data.houseRent.houseFacility
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/houserent/${!this.dataForm.rentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rentId': this.dataForm.rentId || undefined,
                'houUserPhone': this.dataForm.houUserPhone,
                'houseTitle': this.dataForm.houseTitle,
                'housePrice': this.dataForm.housePrice,
                'leaseType': this.dataForm.leaseType,
                'houseLayout': this.dataForm.houseLayout,
                'houseOrientation': this.dataForm.houseOrientation,
                'estateId': this.dataForm.estateId,
                'houseImg': this.dataForm.houseImg,
                'houseSize': this.dataForm.houseSize,
                'floorNum': this.dataForm.floorNum,
                'unitNum': this.dataForm.unitNum,
                'houseNum': this.dataForm.houseNum,
                'leaseTrem': this.dataForm.leaseTrem,
                'houseType': this.dataForm.houseType,
                'houseStatus': this.dataForm.houseStatus,
                'userName': this.dataForm.userName,
                'userPhone': this.dataForm.userPhone,
                'createTime': this.dataForm.createTime,
                'maintenanceTime': this.dataForm.maintenanceTime,
                'houseFacility': this.dataForm.houseFacility
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
